package dimension.starry.block.machine.industry.crusher;

import dimension.starry.StarryDimension;
import dimension.starry.block.BlockRegistryManager;
import dimension.starry.block.machine.industry.IndustryMachineRegister;
import dimension.starry.block.machine.simple.castTable.CastTableContainer;
import dimension.starry.block.typeInterface.MachineTileEntity;
import dimension.starry.item.ItemRegisterManager;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

/**
 * @author 150149
 */
public class CrusherTileEntity extends MachineTileEntity {

    private static final Text TITLE = Text.translatable("block." + StarryDimension.MOD_ID + ".crusher");

    public CrusherTileEntity(BlockPos pos, BlockState state) {
        super(IndustryMachineRegister.CRUSHER.getBlockEntityType(), pos, state, 4, TITLE, 800);
    }

    private static Item getOutput(ItemStack input) {
        Item item = input.getItem();
        if (BlockRegistryManager.TIN_ORE.getBlockItem().equals(item)) {
            return ItemRegisterManager.BROKEN_TIN.getItem();
        }
        return null;
    }

    @Override
    public ScreenHandler createMenu(int sycID, PlayerInventory inventory, PlayerEntity player) {
        return new CastTableContainer(
            IndustryMachineRegister.CRUSHER.getScreenHandlerType(), sycID, inventory, getPos());
    }

    @Override
    public void tick(
        World world, BlockPos pos, BlockState state1, MachineTileEntity blockEntity
    ) {
        if (world == null || world.isClient() || !this.hasSteam()) {
            return;
        }

        ItemStack blockItemStack = this.inventory.getStack(0);
        ItemStack teethItemStack = this.inventory.getStack(1);

        if (blockItemStack.isEmpty()) {
            this.setMachineStatus(false);
            this.resetProgress();
        } else if (getOutput(blockItemStack) == null) {
            this.setMachineStatus(false);
            this.resetProgress();
        } else if (this.getProgress() >= this.getConsumeTime()) {
            ItemStack itemStack = new ItemStack(getOutput(blockItemStack));
            ItemStack dirt = new ItemStack(Items.DIRT);
            if (this.inventory.canInsert(itemStack) && this.inventory.canInsert(dirt)) {
                blockItemStack.setCount(blockItemStack.getCount() - 1);
                this.resetProgress();
                this.inventory.addStack(itemStack);
                this.inventory.addStack(dirt);
            }
        } else if (!this.consumeFuel() && !teethItemStack.isEmpty() &&
            ItemRegisterManager.CRUSHING_TEETH.getItem().equals(teethItemStack.getItem())) {
            teethItemStack.setCount(teethItemStack.getCount() - 1);
            this.addFuel(6400);
            this.consumeFuel();
            this.setMachineStatus(true);
        }
    }

}
